package cn.stronglink.assetmanage.business.check.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;

/**
 * 资产列表
 *
 * @author zww
 *         2018 2018/6/10 18:13
 */
public class CheckOrderAdapter extends RecyclerView.Adapter<CheckOrderAdapter.ViewHolder> {
    private Context mContext;
    private List<Asset> datas;
    private onRecyclerListener listener;

    public CheckOrderAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public CheckOrderAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_check_wait_list, null);
        return new CheckOrderAdapter.ViewHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(CheckOrderAdapter.ViewHolder holder, int position) {
        Asset asset = datas.get(position);
        holder.bindRepo(asset, position);
    }

    @Override
    public int getItemCount() {

        return datas == null ? 0 : datas.size();
    }

    public void setOnItemClickListener(onRecyclerListener listener) {
        this.listener = listener;
    }

    public void notifyDatasChanged(List<Asset> data) {
        this.datas = data;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView tv_check_wait_asset_name;
        private TextView tv_check_wait_sn;
        private TextView tv_check_wait_bind_status;
        private LinearLayout ll_wait_asset;
        private onRecyclerListener listener;

        public ViewHolder(View itemView, onRecyclerListener listener) {
            super(itemView);
            this.listener = listener;
            tv_check_wait_asset_name = (TextView) itemView.findViewById(R.id.tv_check_wait_asset_name);
            tv_check_wait_sn = (TextView) itemView.findViewById(R.id.tv_check_wait_sn);
            tv_check_wait_bind_status = (TextView) itemView.findViewById(R.id.tv_check_wait_bind_status);
            ll_wait_asset = itemView.findViewById(R.id.ll_wait_asset);
            itemView.setOnClickListener(this);
        }

        public void bindRepo(Asset repo, int position) {
            tv_check_wait_asset_name.setText(repo.getAssetName());
            tv_check_wait_sn.setText(repo.getSerialNo());
            if (repo.getIsUpdate()){
                String bindStatus = repo.getRfidNo();
                if (bindStatus == null||"".equals(bindStatus)) {
                    tv_check_wait_bind_status.setText(mContext.getResources().getString(R.string.common_unbind));
                    tv_check_wait_bind_status.setTextColor(mContext.getResources().getColor(R.color.un_bind_red));
                } else {
                    tv_check_wait_bind_status.setText(mContext.getResources().getString(R.string.common_binded));
                    tv_check_wait_bind_status.setTextColor(mContext.getResources().getColor(R.color.bind_green));
                }
            }else{
                tv_check_wait_bind_status.setText("未入库");
                tv_check_wait_bind_status.setTextColor(mContext.getResources().getColor(R.color.un_bind_red));
            }
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(v, getAdapterPosition());
            }
        }
    }

}
